//
//  ExtensionColor.swift
//  StoreManage
//
//  Created by MC on 2017/12/18.
//  Copyright © 2017年 MC. All rights reserved.
//

import UIKit


extension UIColor {
    
    
    public static let MCMain         = MCColor_16(colorStr: MCGetConfiguration_colorMain())  // 主色
    
    public static let MCAssist       = MCColor_16(colorStr: MCGetConfiguration_colorAssist())  // 辅色
    public static let MCTabbarNormal       = MCColor_16(colorStr: MCGetConfiguration_colorTabbarNormal())  // tabbar未选中的颜色
    
    
    // 配色
    public static let MCAssist_A    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_A())
    public static let MCAssist_B    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_B())
    public static let MCAssist_C    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_C())
    public static let MCAssist_D    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_D())
    public static let MCAssist_E    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_E())
    public static let MCAssist_F    = MCColor_16(colorStr: MCGetConfiguration_colorAssist_F())

    
    
    public static let MCWords        = MCColor_16(colorStr: "494949")  // 文字
    
    public static let MCWords_assist = MCColor_16(colorStr: "a2a2a2")  // 次要文字

    
    
    public static let MCBlack        = MCColor_16(colorStr: "333333")
    
    public static let MCGray         = MCColor_16(colorStr: "929292")
    
    public static let MCGray_middle  = MCColor_16(colorStr: "999999")
    
    public static let MCGray_light   = MCColor_16(colorStr: "f8f8f7")
    
    public static let MCRed          = MCColor_16(colorStr: "ff324b")
    
    public static let MCBlue         = MCColor_16(colorStr: "2075C6")

    
    public static let MCPlaceHolder  = MCColor_16(colorStr: "CACACC")  // PlaceHolder颜色
}

// 获取主 / 辅 颜色
private func MCGetConfiguration_colorMain() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_main"] as? String ?? ""
    return key
}
private func MCGetConfiguration_colorAssist() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorTabbarNormal() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_tabbarNormal"] as? String ?? ""
    return key
}

//
private func MCGetConfiguration_colorAssist_A() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_A"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorAssist_B() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_B"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorAssist_C() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_C"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorAssist_D() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_D"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorAssist_E() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_E"] as? String ?? ""
    return key
}

private func MCGetConfiguration_colorAssist_F() -> String {
    let dictM = MCGetInterfaceInfo()
    let key   = dictM["color_assist_F"] as? String ?? ""
    return key
}



//MARK: -RGB颜色
public func MCColor_RGB(r: CGFloat,g: CGFloat,b: CGFloat) -> UIColor {
    if #available(iOS 10.0, *) {
        return UIColor(displayP3Red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    } else {
        // Fallback on earlier versions
        return UIColor.init(red: r/255.0, green: g/255.0, blue: b/255.0, alpha: 1)
    }
}

//MARK: -16进制颜色
public func MCColor_16(colorStr:String) -> UIColor {
    
    var color = UIColor.red
    var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
    
    if cStr.hasPrefix("#") {
        let index = cStr.index(after: cStr.startIndex)
        cStr = String(cStr.prefix(upTo: index))
    }
    
    if cStr.count != 6 {
        return UIColor.black
    }
    //两种不同截取字符串的方法
    let rIndex = cStr.index(cStr.startIndex, offsetBy: 2)
    let rStr = String(cStr.prefix(upTo: rIndex))
    
    let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
    let gStr = cStr[gRange]
    
    let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
    let bStr = cStr.suffix(from: bIndex)
    
    color = UIColor.init(red: CGFloat(Float(changeToInt(numStr: rStr)) / 255), green: CGFloat(Float(changeToInt(numStr: String(gStr))) / 255), blue: CGFloat(Float(changeToInt(numStr: String(bStr))) / 255), alpha: 1)
    
    return color
}

private func changeToInt(numStr:String) -> Int {
    
    let str = numStr.uppercased()
    var sum = 0
    for i in str.utf8 {
        //0-9 从48开始
        sum = sum * 16 + Int(i) - 48
        if i >= 65 {
            //A~Z 从65开始，但初始值为10
            sum -= 7
        }
    }
    return sum
}

// 渐变色
extension CAGradientLayer {
    
    public enum CAGradientDirection {
        case vertical
        case horizontal
    }
    
    /**
     * 使用说明 一定要设置frame
     let gradientLayer = CAGradientLayer()
     let cgColors = [UIColor.white.cgColor,UIColor.red.cgColor]
     gradientLayer = gradientLayer.createLayer(cgColors: cgColors, direction: .horizontal)
     gradientLayer.frame = CGRect.init(x: 0, y: 0, width: 200, height: 30)
     self.layer.insertSublayer(gradientLayer, at: 0)
     */
    
    /**
     * 参数说明
     * cgColors CGColor类型数组
     * direction CAGradientDirection枚举，表示颜色渐变方向
     * locations 每个颜色对应的位置，区间在[0-1]之间.比如三种颜色的集合[A,B,C],设置的区间为[0, 0.2, 1]
     那么A和B均分前0~0.2区间的位置颜色，B和C均分[0.2~1]区间的位置颜色
     */
    public func createLayer(cgColors:[CGColor], direction: CAGradientDirection, locations: [NSNumber] = []) -> CAGradientLayer {
        
        let gradientColors = cgColors
        
        var gradientLocations:[NSNumber] = locations
        
        if locations.isEmpty {
            let arrayM = NSMutableArray()
            let spacing = 1 / Double((cgColors.count - 1))
            
            for i in 0..<cgColors.count {
                let location = Double(i) * spacing
                arrayM.add(NSNumber.init(value: location))
            }
            gradientLocations = arrayM as! [NSNumber]
        }
        
        
        
        //创建CAGradientLayer对象并设置参数
        self.colors = gradientColors
        self.locations = gradientLocations
        
        //设置渲染的起始结束位置（横向渐变）
        if direction == .horizontal {
            self.startPoint = CGPoint(x: 0, y: 0)
            self.endPoint = CGPoint(x: 1, y: 0)
        } else {
            self.startPoint = CGPoint(x: 0, y: 0)
            self.endPoint = CGPoint(x: 0, y: 1)
        }
        return self
    }
}


